package com.helsmanli.holiday.util;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

import com.lmj.holiday.model.WeekDay;

/**
 * <p>
 *	week工具类
 * </p>
 *
 * @author helsmanli
 * @description
 * @date 2022/3/7
 */
public class WeekUtil {

    private static final String REST_MESSAGE = "休息日";
    private static final String WORK_MESSAGE = "工作日";
    private static final Map<Integer, String> CHINESE_WEEK;
    
    static {
    	CHINESE_WEEK = new HashMap<Integer, String>();
    	CHINESE_WEEK.put(1, "周日");
    	CHINESE_WEEK.put(2, "周一");
    	CHINESE_WEEK.put(3, "周二");
    	CHINESE_WEEK.put(4, "周三");
    	CHINESE_WEEK.put(5, "周四");
    	CHINESE_WEEK.put(6, "周五");
    	CHINESE_WEEK.put(7, "周六");
    }

    /**
     * 获取 {year} 年所有的日期 并标注是否为休息日
     * @param year
     * @return
     */
    protected static List<WeekDay> getAllDayByYear(int year)  {
        List<WeekDay> dateList = new ArrayList<>();
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        int day = 1;
        WeekDay restDayModel = null;
        while (true) {
            calendar.set(Calendar.DAY_OF_YEAR, day++);
            if(calendar.get(Calendar.YEAR) == year + 1){
                break;
            }
            LocalDate localDate = calendar.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            restDayModel = new WeekDay(localDate,CHINESE_WEEK.get(week),CHINESE_WEEK.get(week), 
            		getDesc(week),week == 1 || week == 7 ? "1" : "0");
            dateList.add(restDayModel);
        }
        return dateList;
    }
    
    private static String getDesc(int week) {
        return (week == 1 || week == 7) ? REST_MESSAGE : WORK_MESSAGE;
    }

}
